package com.ciwong.epaper.modules.me.ui;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.LogOutUtil;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.BaseConstants;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.util.MeUtil;
import com.ciwong.epaper.modules.me.widget.LineEditText;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import java.io.IOException;

/**
 * 更新用户名Activity Created by res-dlzhou on 2015/9/16.
 */
public class UpdateUserNameActivity extends BaseActivity
{
    private LineEditText mEditText;

    /**
     * 是否须要更换用户名标识
     */
    public static final String IS_NEED_UPDATE_USERNAME = "isNeedUpdateUserName";

    @Override
    protected int setView()
    {
        return R.layout.activity_update_username_layout;
    }

    @Override
    protected void findViews()
    {
        setTitleText(R.string.modify_username);
        setRightBtnText(R.string.update);
        mEditText = (LineEditText) findViewById(R.id.update_username_edit);
    }

    @Override
    protected void initEvent()
    {
        setRightBtnListener(new ViewOnClickListener()
        {
            @Override
            public void avertRepeatOnClick(View v)
            {
                String userName = String.valueOf(mEditText.getText());
                if (isVersionUserName(userName))
                {
                    updateUserInfo(userName);
                }
                else
                {
                    showShakeAnima();
                }
            }
        });
    }

    @Override
    protected void init()
    {
        String curUserName = getUserInfoBase().getRealName();
        if (!TextUtils.isEmpty(curUserName))
        {
            mEditText.setText(curUserName);
            mEditText.setSelection(curUserName.length());
        }
    }

    @Override
    protected void loadData()
    {

    }

    /**
     * 验证名称
     * 
     * @return true为通过
     */
    private boolean isVersionUserName(String userName)
    {
        // 空判断
        if (TextUtils.isEmpty(userName))
        {
            showToastError(R.string.username_not_null);
            return false;
        }

        if (!MeUtil.validateUserName(userName))
        {
            showToastError(R.string.register_name_hint_fail);
            return false;
        }
        return true;
    }

    /**
     * 修改用户姓名
     * 
     * @param userName
     */
    private void updateUserInfo(final String userName)
    {
        showMiddleProgressBar(getTitleText());
        MeDao.getInstance().updateUserInfo(userName, new BaseExtCallBack(UpdateUserNameActivity.this, EApplication.getInstance().getUserInfoBase().getUserId()+"")
        {


            @Override
            public void success(Object data)
            {
                // 修改姓名成功
                getUserInfoBase().setRealName(userName);
                SerializableManager.getInstance().serialize(
                        BaseConstants.SHARE_KEY_USER_INFO_BASE,
                        getUserInfoBase(), false);
                Intent intent = new Intent();
                intent.putExtra(IS_NEED_UPDATE_USERNAME, true);
                setResult(RESULT_OK, intent);
                finish();
                hideMiddleProgressBar();
            }

            @Override
            public void failed(int errorCode, Object data)
            {
                super.failed(errorCode, data);
                hideMiddleProgressBar();
                showToastError(errorCode,data);
            }

            @Override
            public void failed(Object data)
            {
                hideMiddleProgressBar();
                showToastError(R.string.modify_name_fail);
            }

        });
    }

    /**
     * 显示错误提示的动画
     */
    private void showShakeAnima()
    {
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        mEditText.startAnimation(shake);
    }

}
